package cn.zhihong.controller;

import cn.zhihong.common.R;
import cn.zhihong.dto.DishDto;
import cn.zhihong.dto.SetmealDto;
import cn.zhihong.pojo.Category;
import cn.zhihong.pojo.Setmeal;
import cn.zhihong.pojo.SetmealDish;
import cn.zhihong.service.CategoryService;
import cn.zhihong.service.SetmealDishService;
import cn.zhihong.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName SetmealController
 * @Author 庄志宏
 * @Date 2022/4/24 15:49
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * @author 庄志宏
     * @date: 2022/4/24 15:50
     * 套餐管理分页查询
     */
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //创建分页对象
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealLambdaQueryWrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        setmealService.page(setmealPage,setmealLambdaQueryWrapper);
        //拷贝
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        records.forEach(record->{
            SetmealDto setmealDto = new SetmealDto();
            //拷贝
            BeanUtils.copyProperties(record,setmealDto);
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //要进行判断category是否为空，以防空指针错误
            if (category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            setmealDtoList.add(setmealDto);
        });
        setmealDtoPage.setRecords(setmealDtoList);
        return R.success(setmealDtoPage);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/25 15:57
     * 添加套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("套餐添加成功");
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/25 16:00
     * 套餐的停售和启售
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status,Long[] ids){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = status == 1 ?name="启售":"停售";
        List<Setmeal> list = Stream.of(ids).map(id -> {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            return setmeal;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(list);
        return R.success("套餐" + name + "成功");
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/25 16:21
     * 修改套餐中的回显信息
     */
    @GetMapping("/{id}")
    public R<SetmealDto> xianshi(@PathVariable("id") Long id){
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealService.getById(id);
        //拷贝
        BeanUtils.copyProperties(setmeal,setmealDto);
        //条件构造器
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getDishId,id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/25 16:34
     * 修改套餐
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/25 16:46
     * 删除套餐
     */
    @DeleteMapping
    public R<String> delete(Long[] ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/27 15:26
     * 查询套餐
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }
}
